package com.autumn.util.data;

import com.autumn.common.api.application.dto.INextEntityDto;
import com.autumn.common.api.application.dto.output.INextQueryResult;
import com.autumn.mybatis.criterion.EntityQuery;
import com.autumn.mybatis.mapper.IEntityMapper;
import com.autumn.util.ExceptionUtils;
import com.autumn.util.function.FunctionTwoAction;

/**
 * 向后查询生成器
 * 
 * @author 老码农
 *
 *         2018-04-03 11:22:33
 */
public class NextQueryBuilder<TEntity> extends AbstractNextQueryBuilder<NextQueryBuilder<TEntity>> {

	private final EntityQuery<TEntity> query;

	/**
	 * 
	 * @param entityClass
	 */
	public NextQueryBuilder(Class<TEntity> entityClass) {
		ExceptionUtils.checkNotNull(entityClass, "entityClass");
		query = new EntityQuery<>(entityClass);
	}

	@Override
	public EntityQuery<TEntity> getQuery() {
		return this.query;
	}

	/**
	 * 输出结果
	 * 
	 * @param mapper
	 *            仓储
	 * @param resultClass
	 * @return
	 */
	public <TResult extends INextEntityDto> INextQueryResult<TResult> toNextResult(IEntityMapper<TEntity> mapper,
			Class<TResult> resultClass) {
		return this.toNextResult(mapper, resultClass, null);
	}

	/**
	 * 输出结果
	 * 
	 * @param mapper
	 *            仓储
	 * @param resultClass
	 *            结果类型
	 * @param convertDelegate
	 *            转换器
	 * @return
	 */
	public <TResult extends INextEntityDto> INextQueryResult<TResult> toNextResult(IEntityMapper<TEntity> mapper,
			Class<TResult> resultClass, FunctionTwoAction<TEntity, TResult> convertDelegate) {		
		return this.toNextResult(s -> {
			return mapper.selectList(s);
		}, resultClass, convertDelegate);
	}
}
